package com.xcity.record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.xcity.merge.MergeUtil;

public class User extends BaseRecord{
	public static String SQL_PARA = "id, name, nickname, password, salt, creatTime, lastLoginTime, lastLoginIp, lastLogoutTime, phone, channel, channelUid";
	
	public static String COUNT_SQL = "select count(id) from user";
	public static String SELECT_LIMIT = "select * from user limit {0},{1}";
	public static String UPDATE_SET_BAN = "UPDATE `user` SET channelUid = {0} WHERE id = {1};\n";
	public static String SELECT_BAN_NO_PLAYER_USER = "SELECT user.id, user.name, user.channel, user.channelUid FROM `user` WHERE `user`.id NOT IN (SELECT player.userId FROM player) AND `user`.channelUid NOT LIKE '%|abend';\n";
	public static String UPDATE_BAN_NO_PLAYER_USER = "UPDATE `user` SET `user`.channelUid = CONCAT(`user`.channelUid, '|abend') WHERE `user`.id NOT IN (SELECT player.userId FROM player) AND `user`.channelUid NOT LIKE '%|abend';\n";
	
	protected long id;
	protected String name;
	protected String nickname;
	protected String password;
	protected String salt;
	protected Date createTime;
	protected Date lastLoginTime;
	protected String lastLoginIp;
	protected Date lastLogoutTime;
	protected String phone;
	protected String channel;
	protected String channelUid;

	@Override
	public void load(ResultSet rs) throws SQLException {
		this.id = rs.getLong(1);
		this.name = rs.getString(2);
		this.nickname = rs.getString(3);
		this.password = rs.getString(4);
		this.salt = rs.getString(5);
		this.createTime = rs.getTimestamp(6);
		this.lastLoginTime = rs.getTimestamp(7);
		this.lastLoginIp = rs.getString(8);
		this.lastLogoutTime = rs.getTimestamp(9);
		this.phone = rs.getString(10);
		this.channel = rs.getString(11);
		this.channelUid = rs.getString(12);
	}

	@Override
	public String getColumnNames() {
		return SQL_PARA;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(MergeUtil.toSqlString(id));
		sb.append(", ");
		sb.append(MergeUtil.toSqlNameString(name));
		sb.append(", ");
		sb.append(MergeUtil.toSqlNameString(nickname));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(password));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(salt));
		sb.append(", ");
		sb.append(MergeUtil.toDateString(createTime));
		sb.append(", ");
		sb.append(MergeUtil.toDateString(lastLoginTime));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(lastLoginIp));
		sb.append(", ");
		sb.append(MergeUtil.toDateString(lastLogoutTime));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(phone));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(channel));
		sb.append(", ");
		sb.append(MergeUtil.toSqlString(channelUid));
		return sb.toString();
	}

	@Deprecated
	public static List<User> getUserList(Connection conn, int start, int page) throws SQLException{
		List<User> ret = new ArrayList<User>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			String sql = MessageFormat.format(SELECT_LIMIT, start + "", page + "");
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.load(rs);
				ret.add(user);
			}
		}finally{
			if(rs != null){
				rs.close();
			}
			if(ps != null){
				ps.close();
			}
		}
		return ret;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getSalt() {
		return salt;
	}
	public void setSalt(String salt) {
		this.salt = salt;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getLastLoginTime() {
		return lastLoginTime;
	}
	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}
	public String getLastLoginIp() {
		return lastLoginIp;
	}
	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}
	public Date getLastLogoutTime() {
		return lastLogoutTime;
	}
	public void setLastLogoutTime(Date lastLogoutTime) {
		this.lastLogoutTime = lastLogoutTime;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getChannel() {
		return channel;
	}
	public void setChannel(String channel) {
		this.channel = channel;
	}
	public String getChannelUid() {
		return channelUid;
	}
	public void setChannelUid(String channelUid) {
		this.channelUid = channelUid;
	}
}
